package my.ts.code.net;

import java.io.IOException;

import my.ts.code.Config;

public class SessionManager {
	// The actual socket
	SocketHandler socket;

	// in,out thread
	InputThread inputThread;
	OutputThread outputThread;
	
	InComingListener listener;

	public SessionManager() {
		socket = new SocketHandler(my.ts.code.Config.ip,
				my.ts.code.Config.port, my.ts.code.Config.ip);
	}

	// get key
	public void getAccId() {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip + "/webicm/ts/AccountSeq.xml";
		outputThread.queueMessage(me);
	}

	// News

	public void getCategoryNews() {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip + "/webicm/ts/NewsCategory.xml?TSM="
				+ my.ts.code.Config.TSM;
		outputThread.queueMessage(me);
	}

	public void getHeaderNews(String id) {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip
				+ "/webicm/ts/NewsHeader.xml?CategoryID=" + id;
		outputThread.queueMessage(me);
	}

	public void getContentNews(String id) {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip + "/webicm/ts/NewsContent.xml?HeaderID="
				+ id;
		outputThread.queueMessage(me);
	}

	// Story

	public void getCategoryStory() {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip + "/webicm/ts/StoryCategory.xml?TSM="
				+ my.ts.code.Config.TSM;
		outputThread.queueMessage(me);
	}

	public void getHeaderStory(String id) {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip
				+ "/webicm/ts/StoryHeader.xml?CategoryID=" + id;
		outputThread.queueMessage(me);
	}

	public void getContentStory(String id) {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip
				+ "/webicm/ts/StoryContent.xml?HeaderID=" + id;
		outputThread.queueMessage(me);
	}

	// Currency
	public void getCurrency() {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip + "/webicm/ts/Currency.xml";
		outputThread.queueMessage(me);
	}

	public void getGoldPrice() {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip + "/webicm/ts/GoldPrice.xml";
		outputThread.queueMessage(me);

	}

	// lottery
	public void getLotteryProvince() {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip + "/webicm/ts/ProvinceLottery.xml";
		outputThread.queueMessage(me);
	}

	public void getLotteryResult(String id) {
		String me = my.ts.code.Config.startString
				+ my.ts.code.Config.http
				+ my.ts.code.Config.ip
				+ "/webicm/ts/ProvinceResult.xml?ProvinceID="
				+ id + "&TSM=" + Config.TSM;
		outputThread.queueMessage(me);
	}

	// Game
	
	public void getGameAlbum() {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip + "/webicm/ts/GameAlbumAndroid.xml";
		outputThread.queueMessage(me);
	}

//	public void getGameAlbum() {
//		String me = my.ts.code.Config.startString + my.ts.code.Config.http
//				+ my.ts.code.Config.ip + "/webicm/ts/GameAlbum.xml";
//		outputThread.queueMessage(me);
//	}

	public void getGames(String n, String p, String id) {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip + "/webicm/ts/Game.xml?AlbumID=" + id
				+ "&n=" + n + "&p=" + p;
		outputThread.queueMessage(me);
	}

	public void getGameDescription(String id) {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip + "/webicm/ts/GameDetail.xml?GameID="
				+ id;
		outputThread.queueMessage(me);
	}

	// Image
	public void getImageCategories(String n, String p) {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip + "/webicm/ts/ImageCategory.xml?TSM="
				+ my.ts.code.Config.TSM + "&n=" + n + "&p=" + p;
		outputThread.queueMessage(me);
	}

	public void getAlbums(String n, String p, String id) {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip
				+ "/webicm/ts/ImageAlbum.xml?CategoryID=" + id + "&n=" + n
				+ "&p=" + p;
		outputThread.queueMessage(me);
	}

	public void getImageAlbum(String id) {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip + "/webicm/ts/Image.xml?AlbumID=" + id;
		outputThread.queueMessage(me);
	}

	// Video
	public void getVideoAlbum() {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip + "/webicm/ts/VideoAlbum.xml";
		outputThread.queueMessage(me);
	}

	public void getVideos(String n, String p, String id) {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip + "/webicm/ts/Video.xml?AlbumID=" + id
				+ "&n=" + n + "&p=" + p;
		outputThread.queueMessage(me);
	}

	public void getVideoDetails(String sid) {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip + "/webicm/ts/VideoDetail.xml?VideoID="
				+ sid;
		outputThread.queueMessage(me);
	}

	// Music
	public void getMusicCategories() {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip + "/webicm/ts/MusicCategory.xml?TSM="
				+ my.ts.code.Config.TSM;
		outputThread.queueMessage(me);
	}

	public void getMusicAlbum(String n, String p, String id) {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip
				+ "/webicm/ts/MusicAlbum.xml?CategoryID=" + id + "&n=" + n
				+ "&p=" + p;
		outputThread.queueMessage(me);
	}

	public void getMusics(String id) {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip + "/webicm/ts/Music.xml?AlbumID=" + id;
		outputThread.queueMessage(me);
	}

	// Audio
	public void getAudioCategories() {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip + "/webicm/ts/AudioCategory.xml";
		outputThread.queueMessage(me);
	}

	public void getAudioAlbum(String n, String p, String id) {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip
				+ "/webicm/ts/AudioAlbum.xml?CategoryID=" + id + "&n=" + n
				+ "&p=" + p;
		outputThread.queueMessage(me);
	}

	// PostCard
	public void getPostCardAlbum() {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip + "/webicm/ts/PostCardCategory.xml";
		outputThread.queueMessage(me);
	}

	public void getPostCardAlbumDetail(String sid, String n, String p) {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip
				+ "/webicm/ts/PostCardAlbum.xml?CategoryID=" + sid + "&n=" + n
				+ "&p=" + p;
		outputThread.queueMessage(me);
	}

	public void getPostCardDetail(String id) {
		String me = my.ts.code.Config.startString + my.ts.code.Config.http
				+ my.ts.code.Config.ip + "/webicm/ts/PostCard.xml?PostCardID="
				+ id;
		outputThread.queueMessage(me);
	}

	//
	public void connect() throws IOException {
		if (socket.open()){
		// start in/out thread
		inputThread = new InputThread(socket);
		outputThread = new OutputThread(socket);
		// fireEvent(KEvent.CONNECTION);
		} else{
			System.out.println("LisTENER="+listener);
			if (listener != null)
			listener.connectionError(true);
		}
	}

	public void disConnect() throws Exception {
		try {
			socket.close();
			inputThread.stopThread();
			outputThread.stopThread();
		} catch (Exception ex) {
			throw ex;
		}
	}
	public void setListener(InComingListener l) {
		inputThread.setListener(l);
		
	}

	public void setListenerForSocket(InComingListener l) {
		this.listener = l; 
	}
}